<?php
namespace app\Index\Controller;
use app\Tool\Sms\SendTemplateSMS;
use think\Controller;
use think\Request;
use think\Db;
use think\captcha\Captcha;
use think\Session;

class Reg extends Controller
{

    public function index()
    {

        // 显示注册页面
        return view('reg/index', [
            'title' => '注册页面',
        ]);
    }

    public function save(Request $request)
    {

        $data = $request->post();
        $username = $data['user'];
        $image = 'headimage.jpg';
        $birthday = '1900/1/1';
        $list = Db::name('index_login')->field('user, pass,id')->where(array('user' => $username))->find();
        //验证密码
        if ($data['user'] = $list['user']) {
            $this->error('用户已存在');
            exit;
        }
        //接收用户名和密码
        $info = [
            "user" => $username,
            "pass" => md5($data['pass']),
            "email" => $data['email'],
            "Avatar" => $image,
            "birthday" => $birthday,
        ];

        $captcha = input("post.verify", "");
        if (empty($captcha)) {
            return json(['code' => 0, 'data' => '', 'msg' => '验证码不能为空!']);
        }
        if (!captcha_check($captcha)) {
            return $this->error('验证失败');
        }

        $result = Db::name('index_login')->insert($info);

        //重定向页面
        if ($result > 0) {
            return $this->success('注册成功', url('index/index/index'));
        } else {
            return $this->error('注册失败');
        }
    }

    //手机注册方法以及各种验证
    public function doreg(Request $request)
    {
        $code = session::get('code');
        $data = $request->post();
        $userphone = $data['user_phone'];
        $image = 'headimage.jpg';
        $birthday = '1900/1/1';
        $list = Db::name('index_login')->field('user_phone, pass,id')->where(array('user_phone' => $userphone))->find();
        //验证密码
        if ($data['user_phone'] = $list['user_phone']) {
            $this->error('手机用户已存在');
            exit;
        }
        //接收用户名和密码
        $info = [
            "user_phone" =>$userphone,
            "pass" => md5($data['pass1']),
            "Avatar" => $image,
            "birthday" => $birthday,
        ];
        if ( $code != $data['code'] ){
            return $this->error('你的验证码不是火属性，跟我不匹配');
        }

        $result = Db::name('index_login')->insert($info);

        //重定向页面
        if ($result > 0) {
            return $this->success('注册成功', url('index/index/index'));
        } else {
            return $this->error('注册失败');
        }
    }

//    发送短信验证
    public function sendSms(Request $request)
    {
        //$request = Request::instance();
        $phone = $request->param('phone');

        //发送手机验证码
        $code = $this->getCode();
        //把手机验证信息添加到session
        session('code',$code);
        //return $request->phone;
        // 实例化对象
        $sms = new SendTemplateSMS();
        //var_dump($sms);
        //调用对象中的方法
        $result = $sms->send($phone, array($code, 5), 1);
        //返回数据
        return json($result->tojson());
    }
    // 生成手机验证码
    public function getCode()
    {
        $charset = '0123456789';
        $post = strlen($charset) - 1;
        $code = '';
        for ($i = 0; $i < 4; $i++) {
            $code .= $charset[mt_rand(0, $post)];
        }
        return $code;
    }



}


